/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.dao.hibernate;

import com.click.app.dao.BalanceEmployeeDao;
import com.click.app.model.BalanceEmployee;
import com.click.app.model.Employee;
import java.util.List;

/**
 *
 * @author ariel
 */
public class BalanceEmployeeDaoHibernate extends GenericDaoHibernate<BalanceEmployee, Long> implements BalanceEmployeeDao{

    public BalanceEmployeeDaoHibernate() {
        super(BalanceEmployee.class);
    }

    public List<BalanceEmployee> findByEmployee(Employee employee) {
        return getHibernateTemplate().find("from BalanceEmployee where employee=? order by dateBalance", employee);
    }

    public BalanceEmployee lastBalance(Employee employee) {
        List<BalanceEmployee> bEs = getHibernateTemplate().find("from BalanceEmployee where periodPaymentEmployee.employee=? order by dateBalance", employee);
        if(bEs.isEmpty()){
            return new BalanceEmployee(0D);
        }
        return bEs.get(bEs.size() - 1);
    }

    public BalanceEmployee beforeLastBalance(Employee employee) {
        List<BalanceEmployee> bEs = getHibernateTemplate().find("from BalanceEmployee where employee=? order by dateBalance", employee);
        if(bEs.isEmpty() || bEs.size() < 2){
            return new BalanceEmployee(0D);
        }
        return bEs.get(bEs.size() - 2);
    }
    public BalanceEmployee getByIdCutProcess(Long id){
        List<BalanceEmployee> bEs = getHibernateTemplate().find("from BalanceEmployee where cutProcess.id = ?", id);
        if( bEs.isEmpty()){
            return new BalanceEmployee(0D);
        }
        return bEs.get(0);
    }
}
